#include "xmlnode.h"
#include"tree.hh"
#include "XmlNodes.h"
#include <algorithm>

// Returns a xmlnodes collection of the children nodes of this node.
xmlnodes xmlnode::get_child_nodes()
{

    xmlnodes retnodes;
    
    for (tree_node_<std::shared_ptr<xmlnode>> * it = tree_node->first_child;
		 it != NULL; 
         it = it->next_sibling){
        retnodes.push_back(it->data);
    }
    
	return retnodes;
}

// Gets the parent node of this node.
std::shared_ptr<xmlnode> xmlnode::get_parent_node()
{
	if (tree_node->parent == NULL){
		return std::shared_ptr<xmlnode>(NULL);
	}
	return tree_node->parent->data;
}

// Returns a collection of attributes of this node.
xmlattributes xmlnode::get_attributes()
{
	return attributes;
}

// Returns a xmlnodes collection of sibling nodes
xmlnodes xmlnode::get_sibling_nodes()
{
    tree<std::shared_ptr<xmlnode>>::sibling_iterator s_iterator( tree_node );
	
    return xmlnodes(s_iterator.begin(), s_iterator.end());
}

// Returns the innertext of a xmlnode
string xmlnode::get_innertext()
{
	return this->innertext;
}

std::string xmlnode::get_name() {
	return this->name;
}


/*******Friend functions definitions********/

// Sets the attributes for this node
void xmlnode::set_attributes(xmlattributes & xml_attributes)
{
	attributes = xml_attributes;	
}

// Sets the node reference.
void xmlnode::set_node_reference(tree_node_<std::shared_ptr<xmlnode>> * node)
{
	tree_node = node;
}

// Sets the inner text for this node.
void xmlnode::set_innertext(string text)
{
	this->innertext = text;
}

void xmlnode::set_name(const std::string & inStr ) {
	this->name = inStr;
}